import hashlib
import time
import simplejson as json  # pip install simplejson 或者 pip install python-simplejson
from flask import request


def result(code=200, d={}):
    data = dict()  # object.__dict__
    data['code'] = code
    data['data'] = d
    return json.dumps(data, ensure_ascii=False)


def md5(m):
    return hashlib.md5(m.encode()).hexdigest()


def getNowDataTime():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


def after_request(response):
    response.headers["Access-Control-Allow-Origin"] = request.headers.get("Origin") or "http://127.0.0.1:9528"
    response.headers["Access-Control-Allow-Methods"] = "PUT,GET,POST,DELETE"
    response.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization,Accept,Origin,Referer,User-Agent"
    response.headers["Access-Control-Allow-Credentials"] = "true"
    return response
